package com.qcby.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.admin.form.RoleAddForm;
import com.qcby.admin.form.RoleChangeForm;
import com.qcby.admin.form.RoleEditForm;
import com.qcby.admin.form.RolePageForm;
import com.qcby.admin.model.SysRole;
import com.qcby.admin.model.SysRoleMenu;
import com.qcby.admin.service.SysMenuService;
import com.qcby.admin.service.SysRoleMenuService;
import com.qcby.admin.service.SysRoleService;
import com.qcby.admin.vo.RoleVo;
import com.qcby.framwork.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author YYL
 * @since 2024-09-01
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysMenuService menuService;
    @Autowired
    private SysRoleMenuService roleMenuService;

    /*角色列表*/
    @GetMapping("/list")
    public ResponseResult list( RolePageForm roleForm){
       RoleVo roleVo= roleService.roleList(roleForm);
       return ResponseResult.okResult(roleVo);
    }

    /*更新角色状态*/
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody RoleChangeForm roleChangeForm){
        SysRole role=new SysRole();
        role.setId(roleChangeForm.getRoleId());
        role.setStatus(roleChangeForm.getStatus());
        boolean changeStatus=roleService.updateById(role);
        if(changeStatus) return ResponseResult.okResult();
        return ResponseResult.errorResult(500,"更新角色状态失败");
    }

    /*查询角色列表接口*/
    @GetMapping("/listAllRole")
    public ResponseResult listAllRole(){
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<SysRole> list = roleService.list(queryWrapper);
        return ResponseResult.okResult(list);

    }
    /*新增角色*/
    @PostMapping()
    public ResponseResult add(@RequestBody RoleAddForm roleAddForm, HttpSession session){
        //SysUser user = (SysUser) session.getAttribute("user");
        SysRole role=new SysRole();
        BeanUtils.copyProperties(roleAddForm,role);
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        //Long userId = user.getId();
        //System.out.println(userId);
        //role.setCreateBy(userId);
        //添加角色信息
        boolean saveRole=roleService.save(role);
        if(!saveRole) return ResponseResult.errorResult(500,"新增角色失败");

        //添加角色对应的多个菜单
        SysRoleMenu roleMenu=new SysRoleMenu();
        //roleMenu.setRoleId(role.getId());
        List<String> menuIds = roleAddForm.getMenuIds();
        for (String menuId : menuIds) {
            Long menuId1 = Long.valueOf(menuId);
            roleMenu.setMenuId(menuId1);
            roleMenu.setRoleId(role.getId());
            boolean save = roleMenuService.save(roleMenu);
            if (!save) {
                return ResponseResult.errorResult(500,"新增角色对应的菜单失败");
            }
        }
        return ResponseResult.okResult();
    }

    /*回显角色信息*/
    @GetMapping("/{id}")
    public ResponseResult getInfo(@PathVariable Long id){
        SysRole role=roleService.getById(id);
        if(role==null) return ResponseResult.errorResult(500,"角色不存在");
        return ResponseResult.okResult(role);
    }



    /*修改角色*/
    @PutMapping
    public ResponseResult update(@RequestBody RoleEditForm roleEditForm){
        SysRole role=new SysRole();
        BeanUtils.copyProperties(roleEditForm,role);
        role.setUpdateTime(LocalDateTime.now());
        //先更新角色信息
        boolean update=roleService.updateById(role);
        if(!update) return ResponseResult.errorResult(500,"修改角色失败");
        //删除当前角色所有的菜单关联
        roleMenuService.removeByRoleId(role.getId());
        //插入新的菜单关联
        SysRoleMenu roleMenu=new SysRoleMenu();
        List<String> menuIds=roleEditForm.getMenuIds();
        for (String menuId : menuIds) {
            Long menuId1=Long.valueOf(menuId);
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(menuId1);
            Boolean save=roleMenuService.save(roleMenu);
            if(!save) return ResponseResult.errorResult(500,"修改角色对应的菜单失败");
        }
        return ResponseResult.okResult();
    }

    /*删除角色*/
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id){
        boolean del=roleService.removeById(id);
        //删除角色对应的菜单
        roleMenuService.removeByRoleId(id);
        if(!del) return ResponseResult.errorResult(500,"删除角色失败");
        return ResponseResult.okResult();
    }
}

